package com.baizhi.service.impl;

import com.baizhi.dao.CommentDao;
import com.baizhi.dto.CommentDTO;
import com.baizhi.entity.Comment;
import com.baizhi.feign.UserFeign;
import com.baizhi.po.*;
import com.baizhi.service.CommentService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 评论(Comment)表服务实现类
 *
 * @author makejava
 * @since 2021-09-28 14:40:41
 */
@Transactional
@Service("commentService")
public class CommentServiceImpl extends ServiceImpl<CommentDao, Comment> implements CommentService {

    @Autowired
    private CommentDao commentDao;
    @Autowired
    private UserFeign userFeign;


    /**
     * 查询该视频的所有评论，父评论最上面，子评论最下面
     * 先判断是否是父评论
     * 再根据父评论找到对应的子评论
     *
     * @param page
     * @param pageSize
     * @param videoId
     */
    @Override
    public CommentsPO queryAllByVideoId(Integer page, Integer pageSize, Integer videoId) {
        //获取总条数
        LambdaQueryWrapper<Comment> queryWrapper2 = new LambdaQueryWrapper<>();
        queryWrapper2.eq(Comment::getVideoId, videoId);
        Integer integer = commentDao.selectCount(queryWrapper2);


        //创建分页对象
        Page<Comment> page1 = new Page<>(page, pageSize);
        //创建条件对象
        LambdaQueryWrapper<Comment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Comment::getVideoId, videoId);
        //二级id为null的数据
        queryWrapper.isNull(Comment::getParentId);
        //返回数据对象，一级的所有的评论对象
        Page<Comment> commentPage = commentDao.selectPage(page1, queryWrapper);

        //获取到视频对应的数据
        List<Comment> records = commentPage.getRecords();

        //最终返回数据
        CommentsPO commentsPO = new CommentsPO();
        commentsPO.setTotal_count(integer);

        //创建返回数据
        List<CommentPO> list = new ArrayList<>();

        //遍历数据获取对应的二级评论
        for (Comment record : records) {
            //一级的评论的id
            Integer id = record.getId();
            //拼接数据---------------------------------begin--------1--------------
            CommentPO commentPO = new CommentPO();
            ReviewerPO reviewerPO = new ReviewerPO();
            //根据评论对象获取到用户信息
            Integer uid = record.getUid();
            OneUserPO oneUserPO = userFeign.queryOneById(uid);
            //cp
            BeanUtils.copyProperties(oneUserPO, reviewerPO);
            commentPO.setReviewer(reviewerPO);
            commentPO.setId(id);
            commentPO.setContent(record.getContent());
            commentPO.setCreatedAt(record.getCreatedAt());

            //----------------------------------end--------------------
            //用来获取二级评论的数据
            LambdaQueryWrapper<Comment> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(Comment::getVideoId, videoId);
            queryWrapper1.eq(Comment::getParentId, id);
            //返回的数据就是二级评论
            List<Comment> comments = commentDao.selectList(queryWrapper1);

            //创建子评论集合
            List<CommentSubPO> subPOList = new ArrayList<>();

            //数据获取完成[拼接数据]
            for (Comment comment : comments) {
                CommentSubPO commentSubPO = new CommentSubPO();
                ReviewerPO SubreviewerPO = new ReviewerPO();
                //根据评论对象获取到用户信息
                Integer uid1 = comment.getUid();
                OneUserPO oneUserPO1 = userFeign.queryOneById(uid1);
                //cp
                BeanUtils.copyProperties(oneUserPO1, SubreviewerPO);
                //组装
                commentSubPO.setId(comment.getId());
                commentSubPO.setReviewer(SubreviewerPO);
                commentSubPO.setContent(comment.getContent());
                commentSubPO.setParentId(comment.getParentId());
                commentSubPO.setCreatedAt(comment.getCreatedAt());
                subPOList.add(commentSubPO);
            }
            //拼接
            commentPO.setSubComments(subPOList);
            list.add(commentPO);

        }
        commentsPO.setItems(list);

        return commentsPO;

    }

    /**
     * 添加评论
     *
     * @param uid
     * @param videoId
     * @param commentDTO
     */
    @Override
    public void insert(Integer uid, Integer videoId, CommentDTO commentDTO) {
        //判断commentDTO是否有夫类别id，没有则是主的
        if (commentDTO.getParentId() != null) {
            //子类别评论
            Comment entity = new Comment();
            entity.setUid(uid);
            entity.setParentId(commentDTO.getParentId());
            entity.setVideoId(videoId);
            entity.setContent(commentDTO.getContent());
            entity.setCreatedAt(new Date());
            entity.setUpdatedAt(new Date());
            commentDao.insert(entity);
        } else {
            //父类别评论
            Comment entity = new Comment();
            entity.setUid(uid);
            entity.setVideoId(videoId);
            entity.setContent(commentDTO.getContent());
            entity.setCreatedAt(new Date());
            entity.setUpdatedAt(new Date());
            commentDao.insert(entity);
        }
    }
}

